const Koa = require('koa')
const app = new Koa()
const {connect,ininSchemas} = require('./database/init.js')
const mongoose = require('mongoose')
const bodyparser = require('koa-bodyparser')
const cors = require('koa2-cors')
const Router = require('koa-router')


app.use(bodyparser())
app.use(cors())

let user = require('./appApi/user.js')
let goods = require('./appApi/goods.js')

//装载所有子路由
let router = new Router()
router.use('/user',user.routes())
router.use('/goods',goods.routes())

//加载路由中间件
app.use(router.routes())
app.use(router.allowedMethods())


// const { use } = require('vue/types/umd');

;(async ()=>{
  await connect()
  ininSchemas()
  // const User = mongoose.model('User')

  // let oneUser = new User({userName:'long3',password:'1234567'})
  // oneUser.save().then(()=>{
  //   console.log('插入成功')
  // })

  // let user1 = await User.find({}).exec()
  // console.log(user1)

})()

app.use(async(ctx)=>{
  ctx.body = 'index'
})
app.listen(3000,()=>{
  console.log("启动")
})